package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.back.FeedBackH;
import com.ruoyi.information.service.back.IFeedBackHService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 投诉建议Controller
 * 后台
 *
 * @author ruoyi
 * @date 2023-05-09
 */
@RestController
@RequestMapping("/backH")
public class FeedBackHController extends BaseController {
    @Autowired
    private IFeedBackHService feedBackService;

    /**
     * 查询投诉建议列表
     */
    @RequiresPermissions("information:back:list")
    @GetMapping("/list")
    public TableDataInfo list(FeedBackH feedBack) {
        startPage();
        List<FeedBackH> list = feedBackService.selectFeedBackList(feedBack);
        return getDataTable(list);
    }

    /**
     * 导出投诉建议列表
     */
    @RequiresPermissions("information:back:export")
    @Log(title = "投诉建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeedBackH feedBack) {
        List<FeedBackH> list = feedBackService.selectFeedBackList(feedBack);
        ExcelUtil<FeedBackH> util = new ExcelUtil<FeedBackH>(FeedBackH.class);
        util.exportExcel(response, list, "投诉建议数据");
    }

    /**
     * 获取投诉建议详细信息
     */
    @RequiresPermissions("information:back:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(feedBackService.selectFeedBackById(id));
    }

    /**
     * 新增投诉建议
     */
    @RequiresPermissions("information:back:add")
    @Log(title = "投诉建议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeedBackH feedBack) {
        feedBack.setUserId(SecurityUtils.getUserId());
        return toAjax(feedBackService.insertFeedBack(feedBack));
    }

    /**
     * 修改投诉建议
     */
    @RequiresPermissions("information:back:edit")
    @Log(title = "投诉建议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeedBackH feedBack) {
        return toAjax(feedBackService.updateFeedBack(feedBack));
    }

    /**
     * 删除投诉建议
     */
    @RequiresPermissions("information:back:remove")
    @Log(title = "投诉建议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(feedBackService.deleteFeedBackByIds(ids));
    }

    /**
     * 删除投诉建议
     */
    @RequiresPermissions("information:back:remove")
    @Log(title = "投诉建议", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        for (Long id : ids) {
            FeedBackH backH = new FeedBackH();
            backH.setId(id);
            backH.setDeleted(1L);
            backH.setHandleType(2L);
            feedBackService.updateFeedBack(backH);
        }
        return toAjax(1);
    }
}
